package controller;

import entity.Area;
import entity.Category;
import entity.Movie;
import entity.MoviePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.MovieService;

import java.util.Arrays;
import java.util.List;

@Controller
@Transactional
public class MovieController {
    @Autowired
    private MovieService movieService;

    @RequestMapping("/all_movies")
    public String getAllMovies(Integer[] categoryIds, Integer pageNum, Integer pageSize, Model model){
        Integer page = pageNum == null ? 1 : pageNum;
        Integer size = pageSize == null ? 5 : pageSize;
        Integer[] categoryIds1 = categoryIds == null ? new Integer[0] : categoryIds;
        String s = "";
        for (int i = 0; i < categoryIds1.length; i++) {
            if (i > 0){
                s += ",";
            }
            s += categoryIds1[i];
        }
        MoviePage moviePage = movieService.getAllMovies(Arrays.asList(categoryIds1), page, size);
        List<Category> categories = movieService.getAllCategories();

        model.addAttribute("moviePage",moviePage);
        model.addAttribute("categories",categories);
        model.addAttribute("categoryIds",categoryIds1);
        model.addAttribute("s",s);

        return "all_movies";
    }

    @RequestMapping("/goto_add")
    public String gotoAdd(Model model){
        List<Category> categories = movieService.getAllCategories();
        List<Area> areas = movieService.getAllAreas();
        model.addAttribute("categories",categories);
        model.addAttribute("areas",areas);
        return "add_movie";
    }

    @RequestMapping("/add_movie")
    public String addOneMovie(Movie movie, Integer[] categoryId, Model model){
        if (movieService.addOneMovie(movie, Arrays.asList(categoryId)) > 0) {
            model.addAttribute("message", "电影新增成功");
        }
        else {
            model.addAttribute("message", "电影新增失败");
        }

        return "redirect:/all_movies.action";
    }

    @RequestMapping("/goto_set")
    public String gotoSet(Integer id, Model model){
        Movie movie = movieService.getMovieById(id);
        List<Category> categories = movieService.getAllCategories();
        List<Area> areas = movieService.getAllAreas();
        model.addAttribute("movie",movie);
        model.addAttribute("categories",categories);
        model.addAttribute("areas",areas);
        return "set_movie";
    }

    @RequestMapping("/set_movie")
    public String setMovie(Movie movie,Integer[] categoryId, Model model){

        if (movieService.setOneMovie(movie,Arrays.asList(categoryId)) > 0){
            model.addAttribute("message","电影修改成功");
        }else {
            model.addAttribute("message","电影修改失败");
        }
        return "redirect:/all_movies.action";
    }

    @RequestMapping("del_movies")
    public String delAllBooks(Integer[] id) {
        if (id != null) {
            movieService.delMovieByList(Arrays.asList(id));
        }

        return "redirect:/all_movies.action";
    }
}
